package cn.iocoder.yudao.module.erp.controller.admin.secstock;

import cn.hutool.core.date.DateTime;
import cn.iocoder.yudao.module.erp.controller.admin.secaccount.vo.SecHisAccountRespVO;
import cn.iocoder.yudao.module.erp.controller.admin.pristock.vo.PriStockRespVO;
import cn.iocoder.yudao.module.erp.dal.dataobject.checkout.CheckOutItemDO;
import cn.iocoder.yudao.module.erp.dal.dataobject.pristock.PriStockDO;
import jakarta.annotation.security.PermitAll;
import org.springframework.web.bind.annotation.*;
import jakarta.annotation.Resource;
import org.springframework.validation.annotation.Validated;
import org.springframework.security.access.prepost.PreAuthorize;
import io.swagger.v3.oas.annotations.tags.Tag;
import io.swagger.v3.oas.annotations.Parameter;
import io.swagger.v3.oas.annotations.Operation;

import jakarta.validation.*;
import jakarta.servlet.http.*;

import java.math.BigDecimal;
import java.util.*;
import java.io.IOException;

import cn.iocoder.yudao.framework.common.pojo.PageParam;
import cn.iocoder.yudao.framework.common.pojo.PageResult;
import cn.iocoder.yudao.framework.common.pojo.CommonResult;
import cn.iocoder.yudao.framework.common.util.object.BeanUtils;
import static cn.iocoder.yudao.framework.common.pojo.CommonResult.success;

import cn.iocoder.yudao.framework.excel.core.util.ExcelUtils;

import cn.iocoder.yudao.framework.apilog.core.annotation.ApiAccessLog;
import static cn.iocoder.yudao.framework.apilog.core.enums.OperateTypeEnum.*;

import cn.iocoder.yudao.module.erp.controller.admin.secstock.vo.*;
import cn.iocoder.yudao.module.erp.dal.dataobject.secstock.SecStockDO;
import cn.iocoder.yudao.module.erp.service.secstock.SecStockService;

@Tag(name = "管理后台 - ERP 二级库库存")
@RestController
@RequestMapping("/erp/sec-stock")
@Validated
public class SecStockController {

    @Resource
    private SecStockService secStockService;

    /**
     * 根据 type 查询 ERP 二级库库存
     * @param type
     * @return
     */
    @GetMapping("/get-dept-id")
    @Operation(summary = "根据 deptId 查询 ERP 二级库库存")
    @PreAuthorize("@ss.hasPermission('erp:sec-stock:query')")
    public CommonResult<List<SecStockRespVO>> getSecStockByDeptId(@RequestParam("type")List<Integer> type,@RequestParam("Sname") String Sname,@RequestParam("deptId") String deptId) {
        List<SecStockDO> secStock = secStockService.getSecStockByDeptId(type,Sname,deptId);
        return success(BeanUtils.toBean(secStock, SecStockRespVO.class));
    }

    @GetMapping("/getList")
    @Operation(summary = "获得ERP 二级库库存列表")
    @PreAuthorize("@ss.hasPermission('erp:sec-stock:query')")
    public CommonResult<List<SecStockRespVO>> getSecStockList() {
        List<SecStockDO> secStockList = secStockService.getsecStockList();
        return success(BeanUtils.toBean(secStockList, SecStockRespVO.class));
    }

    @GetMapping("/getListByBatch")
    @Operation(summary = "获得ERP 二级库库存列表")
    @PreAuthorize("@ss.hasPermission('erp:sec-stock:query')")
    public CommonResult<List<SecStockRespVO>> getSecStockListByBatch() {
        List<SecStockDO> secStockList = secStockService.getsecStockListByBatch();
        return success(BeanUtils.toBean(secStockList, SecStockRespVO.class));
    }

    @PostMapping("/create")
    @Operation(summary = "创建ERP 二级库库存")
    @PreAuthorize("@ss.hasPermission('erp:sec-stock:create')")
    public CommonResult<Long> createSecStock(@Valid @RequestBody SecStockSaveReqVO createReqVO) {
        return success(secStockService.createSecStock(createReqVO));
    }

    @PostMapping("/virtualCreate")
    @Operation(summary = "高值 虚拟入库 ")
//    @PreAuthorize("@ss.hasPermission('erp:sec-stock:create')")
    public CommonResult<List<Long>> virtualCreate(@Valid @RequestBody List<SecStockSaveReqVO> createReqVOList) {
        return success(secStockService.virtualCreate(createReqVOList));
    }

    @PutMapping("/virtualUpdate")
    @Operation(summary = "高值 销库")
//    @PreAuthorize("@ss.hasPermission('erp:sec-stock:update')")
    public CommonResult<Boolean> virtualUpdate(@Valid @RequestBody List<SecStockSaveReqVO> updateReqVOList) {
        secStockService.virtualUpdate(updateReqVOList);
        return success(true);
    }

    @PostMapping("/virtualPack")
    @Operation(summary = "高值 打包并出库 生成出入库单 ")
//    @PreAuthorize("@ss.hasPermission('erp:sec-stock:create')")
    public CommonResult<Boolean> virtualPack(@RequestBody VirtualPackReqVO virtualPackReqVO) {
        secStockService.virtualPack(virtualPackReqVO);
        return success(true);
    }


    @PutMapping("/update")
    @Operation(summary = "更新ERP 二级库库存")
    @PreAuthorize("@ss.hasPermission('erp:sec-stock:update')")
    public CommonResult<Boolean> updateSecStock(@Valid @RequestBody SecStockSaveReqVO updateReqVO) {
        secStockService.updateSecStock(updateReqVO);
        return success(true);
    }

    @DeleteMapping("/delete")
    @Operation(summary = "删除ERP 二级库库存")
    @Parameter(name = "id", description = "编号", required = true)
    @PreAuthorize("@ss.hasPermission('erp:sec-stock:delete')")
    public CommonResult<Boolean> deleteSecStock(@RequestParam("id") Long id) {
        secStockService.deleteSecStock(id);
        return success(true);
    }

    @GetMapping("/get")
    @Operation(summary = "获得ERP 二级库库存")
    @Parameter(name = "id", description = "编号", required = true, example = "1024")
    @PreAuthorize("@ss.hasPermission('erp:sec-stock:query')")
    public CommonResult<SecStockRespVO> getSecStock(@RequestParam("id") Long id) {
        SecStockDO secStock = secStockService.getSecStock(id);
        return success(BeanUtils.toBean(secStock, SecStockRespVO.class));
    }

    @GetMapping("/page")
    @Operation(summary = "获得ERP 二级库库存分页")
    @PreAuthorize("@ss.hasPermission('erp:sec-stock:query')")
    public CommonResult<PageResult<SecStockRespVO>> getSecStockPage(@Valid SecStockPageReqVO pageReqVO) {
        PageResult<SecStockDO> pageResult = secStockService.getSecStockPage(pageReqVO);
        return success(BeanUtils.toBean(pageResult, SecStockRespVO.class));
    }

    @GetMapping("/pageByDetain")
    @Operation(summary = "获得ERP 二级库库存分页")
    @PreAuthorize("@ss.hasPermission('erp:sec-stock:query')")
    public CommonResult<PageResult<SecStockRespVO>> getSecStockPageByDetain(@Valid SecStockPageReqVO pageReqVO) {
        PageResult<SecStockDO> pageResult = secStockService.getSecStockPageByDetain(pageReqVO);
        return success(BeanUtils.toBean(pageResult, SecStockRespVO.class));
    }

    @GetMapping("/export-excel")
    @Operation(summary = "导出ERP 二级库库存 Excel")
    @PreAuthorize("@ss.hasPermission('erp:sec-stock:export')")
    @ApiAccessLog(operateType = EXPORT)
    public void exportSecStockExcel(@Valid SecStockPageReqVO pageReqVO,
              HttpServletResponse response) throws IOException {
        pageReqVO.setPageSize(PageParam.PAGE_SIZE_NONE);
        List<SecStockDO> list = secStockService.getSecStockPage(pageReqVO).getList();
        // 导出 Excel
        ExcelUtils.write(response, "ERP 二级库库存.xls", "数据", SecStockRespVO.class,
                        BeanUtils.toBean(list, SecStockRespVO.class));
    }

    @GetMapping("/getInboundBatchsByProductId")
    @Operation(summary = "获得ERP 一级库库存批次列表")
    @PreAuthorize("@ss.hasPermission('erp:sec-stock:query')")
    public CommonResult<List<String>> getInboundBatchsByProductId(@RequestParam("productId") Long productId, @RequestParam("productBatch") String productBatch) {
        List<String> batchs = secStockService.getInboundBatchsByProductId(productId,productBatch);
        return success(batchs);
    }

    @GetMapping("/getProductIds")
    @Operation(summary = "获得ERP 二级库库存批次列表")
    @PreAuthorize("@ss.hasPermission('erp:sec-stock:query')")
    public CommonResult<List<SecStockRespVO>> getProductIds() {
        List<SecStockRespVO> productIds = secStockService.getProductIds();
        return success(BeanUtils.toBean(productIds, SecStockRespVO.class));
    }

    @GetMapping("/getIds")
    @Operation(summary = "获得ERP 二级库库存批次列表")
    @PreAuthorize("@ss.hasPermission('erp:sec-stock:query')")
    public CommonResult<List<Integer>> getIds() {
        List<Integer> productIds = secStockService.getIds();
        return success(productIds);
    }

    @GetMapping("/getStandardsByProductName")
    @Operation(summary = "获得ERP er级库库存批次列表")
    @PreAuthorize("@ss.hasPermission('erp:sec-stock:query')")
    public CommonResult<List<String>> getStandardsByProductName(@RequestParam("productName") String productName) {
        List<String> standards = secStockService.getStandardsByProductName(productName);
        return success(standards);
    }

    @GetMapping("/getBatchsByProductId")
    @Operation(summary = "获得ERP er级库库存批次列表")
    @PreAuthorize("@ss.hasPermission('erp:sec-stock:query')")
    public CommonResult<List<String>> getSecStockByProductId(@RequestParam("productId") Long productId) {
        List<String> batchs = secStockService.getBatchsByProductId(productId);
        return success(batchs);
    }

    @GetMapping("/getProductByBatch")
    @Operation(summary = "获得ERP 二级库库存批次列表")
    @PreAuthorize("@ss.hasPermission('erp:sec-stock:query')")
    public CommonResult<SecStockRespVO> getProductByBatch(@RequestParam("productId") Long productId, @RequestParam("productBatch") String productBatch) {
        SecStockDO secStock = secStockService.getProductByBatch(productId,productBatch);
        return success(BeanUtils.toBean(secStock, SecStockRespVO.class));
    }

    @GetMapping("/getStock")
    @Operation(summary = "HIS查询耗材二级库库存接口")
   // @PreAuthorize("@ss.hasPermission('erp:sec-stock:query')")
    public CommonResult<List<SecStockRespVO>> getStock(@RequestParam("deptId") String deptId, @RequestParam("productId") String productId) {
        List<SecStockDO> secStockList = secStockService.getStock(deptId,productId);
        return success(BeanUtils.toBean(secStockList, SecStockRespVO.class));
    }

    @GetMapping("/getStockAccount")
    @Operation(summary = "HIS查询耗材二级库出入库单记录记录")
//    @PreAuthorize("@ss.hasPermission('erp:sec-stock:query')")
    public CommonResult<List<SecHisAccountRespVO>> getStockAccount(
            @RequestParam("deptId") String deptId,
            @RequestParam(value = "productId", required = false) String productId,
            @RequestParam(value = "outInType", required = false) Integer outInType,
            @RequestParam(value = "outInTimeS", required = false) DateTime outInTimeS,
            @RequestParam(value = "outInTimeE", required = false) DateTime outInTimeE) {
        List<SecHisAccountRespVO> secAccountList = secStockService.getStockAccount(deptId,productId,outInType,outInTimeS,outInTimeE);
        return success(secAccountList);
    }

    @PutMapping("/sellProduct")
    @Operation(summary = "HIS售出耗材推送二级库减库存接口")
//    @PreAuthorize("@ss.hasPermission('erp:sec-stock:update')")
    public CommonResult<Map<String, Integer>> sellProduct(@RequestBody List<Map<String, String>> sellList) {
        return success(secStockService.sellProduct(sellList));
    }



}